package com.doudoukeji.service.Activity.mypatient;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.doudoukeji.service.Activity.ConstantValue;
import com.doudoukeji.service.Bean.Token;
import com.doudoukeji.service.Http.MyHttpResponseHandler;
import com.doudoukeji.service.Utils.Util;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.RequestParams;
import com.sanhai.android.mvp.BasePresenter;
import com.sanhai.android.service.http.BusinessClient;
import com.sanhai.android.service.http.Response;

import org.apache.http.Header;

/**
 * @version 2.0
 * @类描述：
 * @项目名称：ddgh
 * @类名称： MyPatientListPresenter
 * @包名称： com.doudoukeji.service.Activity.mypatient
 * @创建人： zhanjunjie
 * @创建时间：2016/3/15 15:54
 * @修改人：
 * @修改时间：2016/3/15 15:54
 * @修改备注： new parent class(用一句话描述修改了什么)
 */
public class MyPatientListPresenter extends BasePresenter {

    private MypatientListView view;
    public Context mcontext;
    public MyPatientListPresenter(Context context, MypatientListView view) {
        super(context, view);
        this.view = view;
    }

    void getPatientList(int pageIndex) {
        RequestParams params = new RequestParams();
        params.put("userId", Token.getId());
        params.put("pageIndex", Util.toString(pageIndex));
        params.put("pageSize", "10");
        params.put("token", Token.getToken());
        Log.d("aaaa", ConstantValue.myclientlist + "?userId" + Token.getId() + "&pageIndex=1&pageSize=10&token=" + Token.getToken());
        BusinessClient.post(ConstantValue.myclientlist, params, new MyHttpResponseHandler(view) {

            @Override
            public void onMyResponse(Response response) {
                //Gson原始解析
                Gson gson = new Gson();
                java.lang.reflect.Type type = new TypeToken<MyPatientListJson>() {
                }.getType();
                MyPatientListJson jsonBean = gson.fromJson(response.getJson(), type);
                if ("1".equals(jsonBean.getStatus())) {
                    view.getMyPatient(jsonBean.getData());
                } else if ("2".equals(jsonBean.getStatus())) {
                    view.getNoData();
                }else if (ConstantValue.OGTOLOGINCODE.equals(jsonBean.getStatus())){
                    mcontext = Token.getMcontext();
                    logOut(response.getStatus());
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
                view.getfail();
            }
        });
    }

    public void logOut(String status) {
        if (ConstantValue.OGTOLOGINCODE.equals(status)) {
            String mess = "发现您的帐号在其它设备上登录";
            String title = "异地登录";

            Intent intent = new Intent();
            intent.setAction(ConstantValue.goToLogin);
            intent.putExtra("message", mess);
            intent.putExtra("msgType", status);
            mcontext.sendBroadcast(intent);
        }
    }
}
